package SandBox.Sonar;

import java.io.PrintWriter;
import java.util.Scanner;

/**
 * Hello world!
 *
 */
public class App 
{
    public static void main( String[] args )
    { 
          	System.out.println(); 
System.out.println(); 
            System.out.println(); 
 System.out.println();
    	System.out.println();
    	System.out.println();
    	PrintWriter pw = new PrintWriter(System.out);
		Scanner scn = new Scanner(System.in);
		int cases = Integer.parseInt(scn.nextLine());
		while(cases-- >0){
			int items = Integer.parseInt(scn.nextLine());
			int[] pr = new int[items];
			int[] wtarr = new int[items];
			for(int idx = 0; idx <items; idx++){
				String[] arr = scn.nextLine().split("\\s");
				pr[idx] = Integer.parseInt(arr[0]);
				wtarr[idx] = Integer.parseInt(arr[1]);
			}
			int mem = Integer.parseInt(scn.nextLine());
			int max = 0;
			while(mem-- >0){
				int maxwt = Integer.parseInt(scn.nextLine());
				int[][] dp = new int[items+1][maxwt+1];
				for(int item=1; item<dp.length; item++){
					for(int wt =1; wt<=maxwt; wt++){
						if(wtarr[item-1] <=wt){
							dp[item][wt] = Math.max(dp[item-1][wt-wtarr[item-1]]+pr[item-1], dp[item-1][wt]);
						}else{
							dp[item][wt] = dp[item-1][wt];
						}
					}
				}
				max += dp[items][maxwt];
			}
			pw.println(max);
		}
		pw.close();
	}
}
